
/**************************** Display Photo Albums*********************************/

function setDivLocation(albumDivID, photoDivID, uploadDivID, element) {
    var albumDiv = document.getElementById(albumDivID);
    var photoDiv = document.getElementById(photoDivID);
    var uploadDiv = document.getElementById(uploadDivID);
    albumDiv.style.left = ((document.getElementById(element).offsetLeft)) + "px";
    albumDiv.style.top = (document.getElementById(element).offsetTop + 20) + "px";

    photoDiv.style.left = ((document.getElementById(element).offsetLeft)) + "px";
    photoDiv.style.top = (document.getElementById(element).offsetTop + 21) + "px";

    uploadDiv.style.left = ((document.getElementById(element).offsetLeft)) + "px";
    uploadDiv.style.top = (document.getElementById(element).offsetTop + 21) + "px";
}

function displayPhotos(albumId, userName, resultDiv) {
    //alert("albumId: " + albumId);
    //alert("user name: " + userName);
    //alert("resultDiv: " + resultDiv);
    var url = "fetchPhotos.asp" + "?cat1=" + albumId + "&cat2=" + userName;
    getHTTPRequestObject();
    if (xmlhttp) {
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = callbackPhotos;
        xmlhttp.send(null);
    }
}

function getHTTPRequestObject() {
    try {
        // Try legacy object first
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            // Try IE implementation now
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        // We must be using a Mozilla-based browser
        // so create a native request object now
        xmlhttp = new XMLHttpRequest();
    }
}

function callbackPhotos() {

    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var strResponseText = xmlhttp.responseText;
            //alert(strResponseText);            
            var endIndex = (strResponseText.indexOf("@"));
            var strPhotos = strResponseText.substr(0, endIndex);
            //alert("strPhotos = " + strPhotos);
            if (strPhotos == "not found") {
                //alert("Your photo search is not found!");
                return false;
            }
            else {
                strResponseText = strResponseText.substr(0, endIndex);

                if (strResponseText != "") {
                    eval("var objResults = " + strResponseText);
                    var divResults = document.getElementById("photoDiv");
                    //alert("Test toi day ne.");
                    if (document.getElementById("tblPhotos")) clearPhotos("photoDiv");

                    var tableRow, tableCell, tableNode, rowFont;
                    var tblTable = document.createElement("table");
                    var tableBody = document.createElement("tbody");
                    var rowFont = document.createElement("font");
                    var strFileName = "";
                    var count = objResults.Results.records.length;
                    var j = 0;
                    var divWidth = 0;

                    tblTable.id = "tblPhotos";
                    tblTable.setAttribute("border", "1px");
                    tblTable.setAttribute("align", "center");
                    tableLength = 0;
                    rIndex = -1;
                    // loop through each of the rows in the result
                    var iPhotoId = 0;
                    var strUserName = "";
                    var strFileName = "";
                    var strTitle = "";
                    var strDescription = "";
                    var strDateTaken = "";
                    var iAlbumID = 0;
                    for (var i = 0; i < count; i++) {
                        iPhotoId = objResults.Results.records[i].photoId;
                        strUserName = objResults.Results.records[i].username;
                        strFileName = objResults.Results.records[i].fileName;
                        strTitle = objResults.Results.records[i].title;
                        strDescription = objResults.Results.records[i].description;
                        strDateTaken = objResults.Results.records[i].dateTaken;
                        iAlbumID = objResults.Results.records[i].albumID;

                        // we'll create each table row and append it to the table body
                        tableRow = document.createElement("tr");
                        tableRow.setAttribute("width", "100");

                        tableRow.onmouseover = function() { this.className = 'mouseOver'; };
                        tableRow.onmouseout = function() { this.className = 'mouseOut'; };
                        tableRow.id = "tr" + i;

                        // build new table cell
                        tableCell = document.createElement("td");
                        // build the cell attributes and functions
                        tableCell.setAttribute("border", "0px");
                        tableCell.setAttribute("height", "30px");
                        //tableCell.onmouseover = function() { this.className = 'mouseOver'; };
                        //tableCell.onmouseout = function() { this.className = 'mouseOut'; };
                        strFileName = "photos/" + strUserName + "/" + strFileName;
                        var img = document.createElement("img");
                        img.setAttribute("src", strFileName);
                        tableNode = img;
                        tableCell.appendChild(tableNode);
                        tableRow.appendChild(tableCell);

                        tableCell = document.createElement("td");
                        // build the cell attributes and functions
                        tableCell.setAttribute("border", "0px");
                        tableCell.setAttribute("height", "30px");
                        //tableCell.onmouseover = function() { this.className = 'mouseOver'; };
                        //tableCell.onmouseout = function() { this.className = 'mouseOut'; };
                        var textNode = document.createTextNode("Title: " + strTitle + "  Description:  " + strDescription + "  Date taken: " + strDateTaken);
                        tableNode = textNode;
                        tableCell.appendChild(tableNode);
                        tableRow.appendChild(tableCell);
                        tableBody.appendChild(tableRow);
                    }

                    // add the table body to the table
                    tblTable.appendChild(tableBody);
                    // add the table to the div tag
                    divResults.appendChild(tblTable);
                    tableLength = i - 1;
                    if (count > 0) {
                        HideDiv("photoAlbumDiv");
                        HideDiv("uploadDiv");
                        ShowDiv("photoDiv");
                    }
                }
            }

        }
    }
}

function clearPhotos(divId) {
    // remove existing rows in results table
    var resultsdiv = document.getElementById(divId);
    var counter = resultsdiv.childNodes.length;
    for (var i = counter - 1; i >= 0; i--) {
        resultsdiv.removeChild(resultsdiv.childNodes[i]);
    }
    HideDiv(divId);
}

function ShowDiv(divId) {
    if (document.layers) document.layers[divId].visibility = "show";
    else document.getElementById(divId).style.visibility = "visible";
}

function HideDiv(divid) {
    if (document.layers) document.layers[divid].visibility = "hide";
    else document.getElementById(divid).style.visibility = "hidden";
}

function DisplayDiv(divOne, divTwo, divThree) {
    HideDiv(divOne);
    HideDiv(divTwo);
    ShowDiv(divThree);

}
/********************** end of displaying photos function ********************/

/**************************** Display Photo Albums*********************************/


function setDivLocation(albumDivID, photoDivID, element) {
    var albumDiv = document.getElementById(albumDivID);
    var photoDiv = document.getElementById(photoDivID);
    albumDiv.style.left = ((document.getElementById(element).offsetLeft)) + "px";
    albumDiv.style.top = (document.getElementById(element).offsetTop + 20) + "px";

    photoDiv.style.left = ((document.getElementById(element).offsetLeft)) + "px";
    photoDiv.style.top = (document.getElementById(element).offsetTop + 21) + "px";
}


function displayPhotosAlbums(albumID, username, resultDiv) {
    ASPajax.LiveASP('fetchPhotos(' + albumID + ',"' + username + '","' + resultDiv + '")', fnCallback);
}


function fnCallback(result) {

    var strResponseText = result;
    //alert(strResponseText);            
    var endIndex = (strResponseText.indexOf("@"));
    var strPhotos = strResponseText.substr(0, endIndex);
    //alert("strPhotos = " + strPhotos);
    if (strPhotos == "not found") {
        //alert("Your photo search is not found!");
        return false;
    }
    else {
        strResponseText = strResponseText.substr(0, endIndex);

        if (strResponseText != "") {

            eval("var objResults = " + strResponseText);
            var divResults = document.getElementById("photoDiv");
            if (document.getElementById("tblPhotos")) clearPhotos("photoDiv");

            var tableRow, tableCell, tableNode, rowFont;
            var tblTable = document.createElement("table");
            var tableBody = document.createElement("tbody");
            var rowFont = document.createElement("font");
            var strFileName = "";
            var count = objResults.Results.records.length;
            var j = 0;
            var divWidth = 0;

            tblTable.id = "tblPhotos";
            tblTable.setAttribute("border", "1px");
            tblTable.setAttribute("align", "center");
            tableLength = 0;
            rIndex = -1;
            // loop through each of the rows in the result
            var iPhotoId = 0;
            var strUserName = "";
            var strFileName = "";
            var strTitle = "";
            var strDescription = "";
            var strDateTaken = "";
            var iAlbumID = 0;
            for (var i = 0; i < count; i++) {
                iPhotoId = objResults.Results.records[i].photoId;
                strUserName = objResults.Results.records[i].username;
                strFileName = objResults.Results.records[i].fileName;
                strTitle = objResults.Results.records[i].title;
                strDescription = objResults.Results.records[i].description;
                strDateTaken = objResults.Results.records[i].dateTaken;
                iAlbumID = objResults.Results.records[i].albumID;

                // we'll create each table row and append it to the table body
                tableRow = document.createElement("tr");
                tableRow.setAttribute("width", "100");

                tableRow.onmouseover = function() { this.className = 'mouseOver'; };
                tableRow.onmouseout = function() { this.className = 'mouseOut'; };
                tableRow.id = "tr" + i;

                // build new table cell
                tableCell = document.createElement("td");
                // build the cell attributes and functions
                tableCell.setAttribute("border", "0px");
                tableCell.setAttribute("height", "30px");
                tableCell.onmouseover = function() { this.className = 'mouseOver'; };
                tableCell.onmouseout = function() { this.className = 'mouseOut'; };
                strFileName = "photos/" + strUserName + "/" + strFileName;
                var img = document.createElement("img");
                img.setAttribute("src", strFileName);
                tableNode = img;
                tableCell.appendChild(tableNode);
                tableRow.appendChild(tableCell);

                tableCell = document.createElement("td");
                // build the cell attributes and functions
                tableCell.setAttribute("border", "0px");
                tableCell.setAttribute("height", "30px");
                tableCell.onmouseover = function() { this.className = 'mouseOver'; };
                tableCell.onmouseout = function() { this.className = 'mouseOut'; };
                var textNode = document.createTextNode("Title: " + strTitle + "  Description:  " + strDescription + "  Date taken: " + strDateTaken);
                tableNode = textNode;
                tableCell.appendChild(tableNode);
                tableRow.appendChild(tableCell);
                tableBody.appendChild(tableRow);
            }

            // add the table body to the table
            tblTable.appendChild(tableBody);
            // add the table to the div tag
            divResults.appendChild(tblTable);
            tableLength = i - 1;

            if (count > 0) {
                HideDiv("photoAlbumDiv");
                ShowDiv("photoDiv");
            }
        }
    }

}


function displayPhotos(albumId, userName, resultDiv) {
    populatePhotos(albumId, userName, resultDiv);
    HideDiv("photoAlbumDiv");
    ShowDiv("photoDiv");
}


function populatePhotos(albumId, userName, resultDiv) {
    //alert("albumId: " + albumId);
    //alert("user name: " + userName);
    //alert("resultDiv: " + resultDiv);
    var url = "fetchPhotos.asp" + "?cat1=" + albumId + "&cat2=" + userName;
    getHTTPRequestObject();
    if (xmlhttp) {
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = callbackPhotos;
        xmlhttp.send(null);
    }
}


function getHTTPRequestObject() {
    try {
        // Try legacy object first
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            // Try IE implementation now
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        // We must be using a Mozilla-based browser
        // so create a native request object now
        xmlhttp = new XMLHttpRequest();
    }
}


function callbackPhotos() {

    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var strResponseText = xmlhttp.responseText;
            //alert(strResponseText);            
            var endIndex = (strResponseText.indexOf("@"));
            var strPhotos = strResponseText.substr(0, endIndex);
            //alert("strPhotos = " + strPhotos);
            if (strPhotos == "not found") {
                //alert("Your photo search is not found!");
                return false;
            }
            else {
                strResponseText = strResponseText.substr(0, endIndex);

                if (strResponseText != "") {
                    eval("var objResults = " + strResponseText);
                    var divResults = document.getElementById("photoDiv");
                    //alert("Test toi day ne.");
                    if (document.getElementById("tblPhotos")) clearPhotos("photoDiv");

                    var tableRow, tableCell, tableNode, rowFont;
                    var tblTable = document.createElement("table");
                    var tableBody = document.createElement("tbody");
                    var rowFont = document.createElement("font");
                    var strFileName = "";
                    var count = objResults.Results.records.length;
                    var j = 0;
                    var divWidth = 0;

                    tblTable.id = "tblPhotos";
                    tblTable.setAttribute("border", "1px");
                    tblTable.setAttribute("align", "center");
                    tableLength = 0;
                    rIndex = -1;
                    // loop through each of the rows in the result
                    var iPhotoId = 0;
                    var strUserName = "";
                    var strFileName = "";
                    var strTitle = "";
                    var strDescription = "";
                    var strDateTaken = "";
                    var iAlbumID = 0;
                    for (var i = 0; i < count; i++) {
                        iPhotoId = objResults.Results.records[i].photoId;
                        strUserName = objResults.Results.records[i].username;
                        strFileName = objResults.Results.records[i].fileName;
                        strTitle = objResults.Results.records[i].title;
                        strDescription = objResults.Results.records[i].description;
                        strDateTaken = objResults.Results.records[i].dateTaken;
                        iAlbumID = objResults.Results.records[i].albumID;

                        // we'll create each table row and append it to the table body
                        tableRow = document.createElement("tr");
                        tableRow.setAttribute("width", "100");

                        tableRow.onmouseover = function() { this.className = 'mouseOver'; };
                        tableRow.onmouseout = function() { this.className = 'mouseOut'; };
                        tableRow.id = "tr" + i;

                        // build new table cell
                        tableCell = document.createElement("td");
                        // build the cell attributes and functions
                        tableCell.setAttribute("border", "0px");
                        tableCell.setAttribute("height", "30px");
                        //tableCell.onmouseover = function() { this.className = 'mouseOver'; };
                        //tableCell.onmouseout = function() { this.className = 'mouseOut'; };
                        strFileName = "photos/" + strUserName + "/" + strFileName;
                        var img = document.createElement("img");
                        img.setAttribute("src", strFileName);
                        tableNode = img;
                        tableCell.appendChild(tableNode);
                        tableRow.appendChild(tableCell);

                        tableCell = document.createElement("td");
                        // build the cell attributes and functions
                        tableCell.setAttribute("border", "0px");
                        tableCell.setAttribute("height", "30px");
                        //tableCell.onmouseover = function() { this.className = 'mouseOver'; };
                        //tableCell.onmouseout = function() { this.className = 'mouseOut'; };
                        var textNode = document.createTextNode("Title: " + strTitle + "  Description:  " + strDescription + "  Date taken: " + strDateTaken);
                        tableNode = textNode;
                        tableCell.appendChild(tableNode);
                        tableRow.appendChild(tableCell);
                        tableBody.appendChild(tableRow);
                    }

                    // add the table body to the table
                    tblTable.appendChild(tableBody);
                    // add the table to the div tag
                    divResults.appendChild(tblTable);
                    tableLength = i - 1;
                }
            }

        }
    }
}


function clearPhotos(divId) {
    // remove existing rows in results table
    var resultsdiv = document.getElementById(divId);
    var counter = resultsdiv.childNodes.length;
    for (var i = counter - 1; i >= 0; i--) {
        resultsdiv.removeChild(resultsdiv.childNodes[i]);
    }
    HideDiv(divId);
}

function ShowDiv(divId) {
    if (document.layers) document.layers[divId].visibility = "show";
    else document.getElementById(divId).style.visibility = "visible";
}

function HideDiv(divid) {
    if (document.layers) document.layers[divid].visibility = "hide";
    else document.getElementById(divid).style.visibility = "hidden";
}

function DisplayDiv(divOne, divTwo) {
    HideDiv(divOne);
    ShowDiv(divTwo);

}
/********************** end of displaying photos function ********************/
